package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.partolStore.RepairRecordsBo;
import com.ruoyi.wms.domain.vo.RepairRecordsVo;
import com.ruoyi.wms.domain.entity.partolStore.RepairRecords;
import com.ruoyi.wms.mapper.RepairRecordsMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 维修记录Service业务层处理
 *
 * @author xhr
 * @date 2024-12-04
 */
@RequiredArgsConstructor
@Service
public class RepairRecordsService {

    private final RepairRecordsMapper repairRecordsMapper;

    /**
     * 查询维修记录
     */
    public RepairRecordsVo queryById(Long id){
        return repairRecordsMapper.selectVoById(id);
    }

    /**
     * 查询维修记录列表
     */
    public TableDataInfo<RepairRecordsVo> queryPageList(RepairRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RepairRecords> lqw = buildQueryWrapper(bo);
        Page<RepairRecordsVo> result = repairRecordsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修记录列表
     */
    public List<RepairRecordsVo> queryList(RepairRecordsBo bo) {
        LambdaQueryWrapper<RepairRecords> lqw = buildQueryWrapper(bo);
        return repairRecordsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RepairRecords> buildQueryWrapper(RepairRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RepairRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipmentId() != null, RepairRecords::getEquipmentId, bo.getEquipmentId());
        lqw.between(params.get("beginRepairTime") != null && params.get("endRepairTime") != null,
            RepairRecords::getRepairTime ,params.get("beginRepairTime"), params.get("endRepairTime"));
        lqw.eq(bo.getRepairStatus() != null, RepairRecords::getRepairStatus, bo.getRepairStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairer()), RepairRecords::getRepairer, bo.getRepairer());
        return lqw;
    }

    /**
     * 新增维修记录
     */
    public void insertByBo(RepairRecordsBo bo) {
        RepairRecords add = MapstructUtils.convert(bo, RepairRecords.class);
        repairRecordsMapper.insert(add);
    }

    /**
     * 修改维修记录
     */
    public void updateByBo(RepairRecordsBo bo) {
        RepairRecords update = MapstructUtils.convert(bo, RepairRecords.class);
        repairRecordsMapper.updateById(update);
    }

    /**
     * 批量删除维修记录
     */
    public void deleteByIds(Collection<Long> ids) {
        repairRecordsMapper.deleteBatchIds(ids);
    }
}
